<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/3/9
 * Time: 14:03
 */

namespace app\index\controller;


use app\index\validate\Valid;

class Kqgl extends Common
{
    /**
     * 我的考勤记录
     */
    public function myclockinginrecord(){
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $this->assign([
            'start'=>$start,
            'end'=>$end
        ]);

        $where = [];
        $where['em_id'] = session('emplyee_id');
        if($start&&!$end){
            $where['date'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['date'] = ['<',$end];
        }elseif($start&&$end){
            $where['date'] = [['>',$start],['<',$end],'and'];
        }
        $count = db('yd_kq_log')->where($where)->count();
        $data = db('yd_kq_log')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k] = $v;
        }
        $this->assign('data',$data);
        $this->assign('page',$page);

        //我的考勤信息
        $wdkqxx = db('yd_kq_em t')->join('yd_kq t2','t2.id=t.kq_id')->where('xm',session('emplyee_id'))->field('xm,sbdd,sbsj,sbjd,sbwd,xbdd,xbsj,xbjd,xbwd')->find();
        $this->assign('wdkqxx',$wdkqxx);

        return $this->fetch();
    }

    /**
     * 下属考勤信息
     */
    public function xiashuclockingrecord(){
        $em_name = $this->request->param('em_name');
        $em_de = $this->request->param('em_de');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $this->assign([
            'start'=>$start,
            'end'=>$end,
            'em_name'=>$em_name,
            'em_de'=>$em_de
        ]);

        $where = [];
        //查询我负责的部门下属的所有人员
        $bmids = db('department t')->join('department_employee t2','t.id=t2.department_id')->where(['t.principal'=>session('emplyee_id'),'t2.index'=>1])->column('t2.employee_id');
        $where['em_id'] = ['in',$bmids];
        if($em_name){
            $where['em_name'] = ['like',"%{$em_name}%"];
        }
        if($em_de){
            $where['em_de'] = ['like',"%{$em_de}%"];
        }
        if($start&&!$end){
            $where['date'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['date'] = ['<',$end];
        }elseif($start&&$end){
            $where['date'] = [['>',$start],['<',$end],'and'];
        }
        $count = db('yd_kq_log')->where($where)->count();
        $data = db('yd_kq_log')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k] = $v;
        }
        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch();
    }

    /**
     * 月考勤记录
     */
    public function monthclockrecord(){
        $ny = $this->request->param('ny');
        $bm = $this->request->param('bm');
        $this->assign('ny',$ny);
        $this->assign('bm',$bm);
        if($ny){
            $ny = $ny;
        }else{
            $ny = date('Y-m');
        }
        //统计当前月份的所有人员的考勤情况
        //查询所有人员
        $cnt = db('employee')->field('id,em_name')->count();
        $ry = db('employee')->field('id,em_name')->paginate(10,$cnt);
        $page = $ry->render();
        $ry = $ry->items();
        foreach($ry as $k=>$v){
            $v['ny'] = $ny;
            $v['bm'] = db('department t')->join('department_employee t2','t.id=t2.department_id')->where(['t2.employee_id'=>$v['id'],'t2.index'=>1])->value('t.de_name');
            $v['job'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where(['t2.employee_id'=>$v['id']])->value('t.job_name');
            $v['sjcqts'] = db('yd_kq_log')->where(['date'=>[['>=',$ny.'-1'],['<=',date('Y-m-d',strtotime($ny.'-1 +1 month -1 day'))],'and'],'em_id'=>$v['id']])->count();
            $v['cdcs'] = db('yd_kq_log t')
                ->join('yd_kq_em t2','t2.xm=t.em_id')
                ->join('yd_kq t3','t3.id=t2.kq_id')
                ->where('t.sbsj>t3.sbsj')->where('em_id',$v['id'])
                ->count();
            $cdsc = db('yd_kq_log t')
                ->join('yd_kq_em t2','t2.xm=t.em_id')
                ->join('yd_kq t3','t3.id=t2.kq_id')
                ->where('t.sbsj>t3.sbsj')->where('em_id',$v['id'])
                ->field('t.sbsj sj1,t3.sbsj sj2')->select();
            $cha = 0;
            foreach($cdsc as $k1=>$v1){
                $cha += strtotime($v1['sj1'])-strtotime($v1['sj2']);
            }
            $v['cdsc'] = $cha/60;
            $v['ztcs'] = db('yd_kq_log t')
                ->join('yd_kq_em t2','t2.xm=t.em_id')
                ->join('yd_kq t3','t3.id=t2.kq_id')
                ->where('t.xbsj < t3.xbsj')->where('em_id',$v['id'])
                ->count();
            $cdsc = db('yd_kq_log t')
                ->join('yd_kq_em t2','t2.xm=t.em_id')
                ->join('yd_kq t3','t3.id=t2.kq_id')
                ->where('t.xbsj < t3.xbsj')->where('em_id',$v['id'])
                ->field('t.xbsj sj1,t3.xbsj sj2')->select();
            $cha = 0;
            foreach($cdsc as $k1=>$v1){
                $cha += strtotime($v1['sj1'])-strtotime($v1['sj2']);
            }
            $v['ztsc'] = $cha/60;
            $v['sbsj1'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-1'])->value('sbsj');
            $v['xbsj1'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-1'])->value('xbsj');
            $v['sbsj2'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-2'])->value('sbsj');
            $v['xbsj2'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-2'])->value('xbsj');
            $v['sbsj3'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-3'])->value('sbsj');
            $v['xbsj3'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-3'])->value('xbsj');
            $v['sbsj4'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-4'])->value('sbsj');
            $v['xbsj4'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-4'])->value('xbsj');
            $v['sbsj5'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-5'])->value('sbsj');
            $v['xbsj5'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-5'])->value('xbsj');
            $v['sbsj6'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-6'])->value('sbsj');
            $v['xbsj6'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-6'])->value('xbsj');
            $v['sbsj7'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-7'])->value('sbsj');
            $v['xbsj7'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-7'])->value('xbsj');
            $v['sbsj8'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-8'])->value('sbsj');
            $v['xbsj8'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-8'])->value('xbsj');
            $v['sbsj9'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-9'])->value('sbsj');
            $v['xbsj9'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-9'])->value('xbsj');
            $v['sbsj10'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-10'])->value('sbsj');
            $v['xbsj10'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-10'])->value('xbsj');
            $v['sbsj11'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-11'])->value('sbsj');
            $v['xbsj11'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-11'])->value('xbsj');
            $v['sbsj12'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-12'])->value('sbsj');
            $v['xbsj12'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-12'])->value('xbsj');
            $v['sbsj13'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-13'])->value('sbsj');
            $v['xbsj13'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-13'])->value('xbsj');
            $v['sbsj14'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-14'])->value('sbsj');
            $v['xbsj14'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-14'])->value('xbsj');
            $v['sbsj15'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-15'])->value('sbsj');
            $v['xbsj15'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-15'])->value('xbsj');
            $v['sbsj16'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-16'])->value('sbsj');
            $v['xbsj16'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-16'])->value('xbsj');
            $v['sbsj17'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-17'])->value('sbsj');
            $v['xbsj17'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-17'])->value('xbsj');
            $v['sbsj18'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-18'])->value('sbsj');
            $v['xbsj18'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-18'])->value('xbsj');
            $v['sbsj19'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-19'])->value('sbsj');
            $v['xbsj19'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-19'])->value('xbsj');
            $v['sbsj20'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-20'])->value('sbsj');
            $v['xbsj20'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-20'])->value('xbsj');
            $v['sbsj21'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-21'])->value('sbsj');
            $v['xbsj21'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-21'])->value('xbsj');
            $v['sbsj22'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-22'])->value('sbsj');
            $v['xbsj22'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-22'])->value('xbsj');
            $v['sbsj23'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-23'])->value('sbsj');
            $v['xbsj23'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-23'])->value('xbsj');
            $v['sbsj24'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-24'])->value('sbsj');
            $v['xbsj24'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-24'])->value('xbsj');
            $v['sbsj25'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-25'])->value('sbsj');
            $v['xbsj25'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-25'])->value('xbsj');
            $v['sbsj26'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-26'])->value('sbsj');
            $v['xbsj26'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-26'])->value('xbsj');
            $v['sbsj27'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-27'])->value('sbsj');
            $v['xbsj27'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-27'])->value('xbsj');
            $v['sbsj28'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-28'])->value('sbsj');
            $v['xbsj28'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-28'])->value('xbsj');
            $v['sbsj29'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-29'])->value('sbsj');
            $v['xbsj29'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-29'])->value('xbsj');
            $v['sbsj30'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-30'])->value('sbsj');
            $v['xbsj30'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-30'])->value('xbsj');
            $v['sbsj31'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-31'])->value('sbsj');
            $v['xbsj31'] = db('yd_kq_log')->where(['em_id'=>$v['id'],'date'=>$ny.'-31'])->value('xbsj');
            $ry[$k] = $v;
        }
        $this->assign('ry',$ry);
        $this->assign('page',$page);

        return $this->fetch();
    }

    /**
     * 考勤信息设置
     */
    public function kqxxsz(){
        $fzmc = $this->request->param('fzmc');
        $ygxm = $this->request->param('ygxm');
        $this->assign('fzmc',$fzmc);
        $this->assign('ygxm',$ygxm);
        $where = [];
        $where['default'] = 0;
        if($fzmc){
            $where['title'] = $fzmc;
        }
        if($ygxm){
            $where['id'] = ['in',"select kq_id from yd_kq_em t join employee t2 on t2.id=t.xm where t2.em_name like '%".$ygxm."%'"];
        }
        $count = db('yd_kq')->where($where)->count();
        $data = db('yd_kq')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['ygxm'] = db('employee')->where('id',$v['ygxm'])->value('em_name');
            $v['cuser'] = db('employee')->where('id',$v['cuser'])->value('em_name');
            $v['cnt'] = db('yd_kq_em')->where(['kq_id'=>$v['id']])->count();
            $data[$k] = $v;
        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        //查询默认考勤信息
        $mrkq = db('yd_kq')->where('default',1)->find();
        $mrkq['cuser_name'] = db('employee')->where('id',$mrkq['cuser'])->value('em_name');
        $this->assign('mrkq',$mrkq);
        return $this->fetch();
    }

    /**
     * 新增考勤分组
     */
    public function add(){
        if($_POST){
            $data = [];
            $data['title'] = $this->request->post('title');
            $ygxm = $this->request->post('ygxm/a');
            $data['sbdd'] = $this->request->post('sbdd');
            $data['sbsj'] = $this->request->post('sbsj');
            $data['sbjd'] = $this->request->post('sbjd');
            $data['sbwd'] = $this->request->post('sbwd');
            $data['xbdd'] = $this->request->post('xbdd');
            $data['xbsj'] = $this->request->post('xbsj');
            $data['xbwd'] = $this->request->post('sbwd');
            $data['xbjd'] = $this->request->post('sbjd');
            $validate = new Valid([
                'title|分组名称'  => 'require',
//                'ygxm|员工姓名'  => 'require',
                'sbdd|上班地点'  => 'require',
                'sbsj|上班时间'  => 'require',
                'sbjd|上班经度'  => 'require',
                'sbwd|上班纬度'  => 'require',
                'xbdd|下班地点'  => 'require',
                'xbsj|下班时间'  => 'require',
                'xbwd|下班纬度'  => 'require',
                'xbjd|下班经度'  => 'require',
            ]);
            if(!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }
            if(count($ygxm)<=0){
                echo json_encode(['success'=>false,
                    'msg'=>'请选择员工']);die;
            }

            $data['cuser'] = session('employee_id');
            $data['ctime'] = date('Y-m-d');

            $bool = db('yd_kq')->insertGetId($data);
            if(false!==$bool){
                $insert = [];
                foreach($ygxm as $k=>$v){
                    $tmp = [];
                    $tmp['xm'] = $v;
                    $tmp['zbm'] = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['t2.index'=>1,'t2.employee_id'=>$v])->value('t.de_name');
                    $tmp['gw'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where(['t2.employee_id'=>$v])->value('t.job_name');
                    $tmp['sjhm'] = db('employee')->where(['id'=>$v])->value('mobile');
                    $tmp['email'] = db('employee')->where(['id'=>$v])->value('email');
                    $tmp['kq_id'] = $bool;
                    $insert[] = $tmp;
                }
                db('yd_kq_em')->insertAll($insert);
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
    }

    /**
     * 默认考勤设置
     */
    public function mrkqsz(){
        if($_POST){
            $data = [];
            $data['sbdd'] = $this->request->post('sbdd');
            $data['sbsj'] = $this->request->post('sbsj');
            $data['sbjd'] = $this->request->post('sbjd');
            $data['sbwd'] = $this->request->post('sbwd');
            $data['xbdd'] = $this->request->post('xbdd');
            $data['xbsj'] = $this->request->post('xbsj');
            $data['xbwd'] = $this->request->post('sbwd');
            $data['xbjd'] = $this->request->post('sbjd');
            $validate = new Valid([
//                'ygxm|员工姓名'  => 'require',
                'sbdd|上班地点'  => 'require',
                'sbsj|上班时间'  => 'require',
                'sbjd|上班经度'  => 'require',
                'sbwd|上班纬度'  => 'require',
                'xbdd|下班地点'  => 'require',
                'xbsj|下班时间'  => 'require',
                'xbwd|下班纬度'  => 'require',
                'xbjd|下班经度'  => 'require',
            ]);
            if(!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['cuser'] = session('employee_id');
            $data['ctime'] = date('Y-m-d');
            $data['default'] = 1;

            $cnt = db('yd_kq')->where('default',1)->count();
            if($cnt>0){
                $bool = db('yd_kq')->where('default',1)->update($data);
            }else{
                $bool = db('yd_kq')->insertGetId($data);
            }

            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
    }

    //查询考勤
    public function update(){
        if($_POST){
            $data = [];
            $data['title'] = $this->request->post('title');
            $ygxm = $this->request->post('ygxm/a');
            $data['sbdd'] = $this->request->post('sbdd');
            $data['sbsj'] = $this->request->post('sbsj');
            $data['sbjd'] = $this->request->post('sbjd');
            $data['sbwd'] = $this->request->post('sbwd');
            $data['xbdd'] = $this->request->post('xbdd');
            $data['xbsj'] = $this->request->post('xbsj');
            $data['xbwd'] = $this->request->post('sbwd');
            $data['xbjd'] = $this->request->post('sbjd');
            $data['id'] = $this->request->post('id');
            $validate = new Valid([
                'title|分组名称'  => 'require',
//                'ygxm|员工姓名'  => 'require',
                'sbdd|上班地点'  => 'require',
                'sbsj|上班时间'  => 'require',
                'sbjd|上班经度'  => 'require',
                'sbwd|上班纬度'  => 'require',
                'xbdd|下班地点'  => 'require',
                'xbsj|下班时间'  => 'require',
                'xbwd|下班纬度'  => 'require',
                'xbjd|下班经度'  => 'require',
            ]);
            if(!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }
            if(count($ygxm)<=0){
                echo json_encode(['success'=>false,
                    'msg'=>'请选择员工']);die;
            }

            $data['cuser'] = session('employee_id');
            $data['ctime'] = date('Y-m-d');

            $bool = db('yd_kq')->update($data);
            if(false!==$bool){
                $insert = [];
                foreach($ygxm as $k=>$v){
                    $tmp = [];
                    $tmp['xm'] = $v;
                    $tmp['zbm'] = db('department t')->join('department_employee t2','t2.department_id=t.id')->where(['t2.index'=>1,'t2.employee_id'=>$v])->value('t.de_name');
                    $tmp['gw'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where(['t2.employee_id'=>$v])->value('t.job_name');
                    $tmp['sjhm'] = db('employee')->where(['id'=>$v])->value('mobile');
                    $tmp['email'] = db('employee')->where(['id'=>$v])->value('email');
                    $tmp['kq_id'] = $bool;
                    $insert[] = $tmp;
                }
                db('yd_kq_em')->insertAll($insert);
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }

        $id = $this->request->get('id');
        $data = db('yd_kq')->where('id',$id)->find();
        $ygxx = db('yd_kq_em')->where('kq_id',$id)->column('xm');
        $str = '';
        foreach($ygxx as $k=>$v){
            $t = $v;
            $v = [];
            $v['id'] = $t;
            $v['title'] = db('employee')->where('id',$t)->value('em_name');
            $ygxx[$k] = $v;
            $str .= '<span class="ren" data-id="'.$v['id'].'">'.$v['title'].'<span class="close">×</span><input type="hidden" name="ygxm[]" value="'.$v['id'].'"></span>';
        }

        $data['ygxx'] = $str;
        $this->assign('data',$data);
        echo json_encode($data);

    }

    /**
     * 删除
     */
    public function delete(){
        $id = $this->request->param('id');
        $bool = db('yd_kq_em')->where('kq_id',$id)->delete();
        $bool2 = db('yd_kq')->where('id',$id)->delete();
        if(false!==$bool&&false!==$bool2){
            echo json_encode(['success'=>true,'msg'=>'ok']);die;
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);die;
        }
    }

    /**
     *考勤分组职员表
     */
    public function clockingfzlidt(){
        $id = $this->request->param('id');
        $xm = $this->request->param('xm');
        $this->assign('xm',$xm);
        $zbm = $this->request->param('zbm');
        $this->assign('zbm',$zbm);
        $gw = $this->request->param('gw');
        $this->assign('gw',$gw);
        $where = [];
        $where['kq_id'] = $id;
        if($xm)
            $where['xm'] = $xm;
        if($zbm)
            $where['zbm'] = $zbm;
        if($gw)
            $where['gw'] = $gw;
        $count = db('yd_kq_em')->where($where)->count();
        $data = db('yd_kq_em')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();

        foreach($data as $k=>$v){
            $v['xm_name'] = db('employee')->where('id',$v['xm'])->value('em_name');
            $data[$k] = $v;
        }
        $this->assign('data',$data);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 业务员考勤月报表
     */
    public function salesmanmkqbb(){
        $yf = $this->request->param('yf');
        $xm = $this->request->param('xm');
        $ssbm = $this->request->param('ssbm');
        $ssfq = $this->request->param('ssfq');
        if(!$yf)
            $yf = date('Y-m');
        $this->assign('yf',$yf);
        $this->assign('xm',$xm);
        $this->assign('ssbm',$ssbm);
        $this->assign('ssfq',$ssfq);
        $where = [];
        $where['t3.date'] = [['>=',$yf.'-1'],['<=',date('Y-m-d',strtotime($yf.'-1 +1 month -1 day'))],'and'];
        if($xm){
            $where['t3.em_name'] = ['like','%'.$xm.'%'];
        }
        if($ssbm){
            $where['t3.em_de'] = ['like','%'.$ssbm.'%'];
        }
        $cnt = db('yd_kq_em t')->join('yd_kq t2','t2.id=t.kq_id')->join('yd_kq_log t3','t3.em_id=t.xm')->group('t.xm')->field('t.xm,t3.em_de,t.gw,t.sjhm')->where($where)->count();

        $data = db('yd_kq_em t')->join('yd_kq t2','t2.id=t.kq_id')->join('yd_kq_log t3','t3.em_id=t.xm')->group('t.xm')->field('t.xm,t3.em_de,t.gw,t.sjhm,count(*) ts')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['xm'] = db('employee')->where('id',$v['xm'])->value('em_name');
            $v['yhm'] = db('users')->where('employee_id',$v['xm'])->value('login_name');
            $v['yf'] = $yf;
            $data[$k] = $v;
        }
        $this->assign('page',$page);
        $this->assign('data',$data);

        return $this->fetch();
    }

}